//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{

	public class RatingListing : GalleryThemedControl
	{

		#region Child Controls

		private IText AverageRating;
		private IText TotalVotes;
		private GalleryRatePost RatePost;

		#endregion

		#region Public Properties

		[DefaultValue( null )]
		public virtual string ApplicationKey
		{
			get
			{
				Object state = ViewState["ApplicationKey"];
				if(state != null)
					return (string)state;
				return null;
			}
			set { ViewState["ApplicationKey"] = value; }
		}

		[DefaultValue( -1 )]
		public virtual int PictureID
		{
			get
			{
				Object state = ViewState["PictureID"];
				if(state != null)
					return (int)state;
				return -1;
			}
			set { ViewState["PictureID"] = value; }
		}

		private GalleryPost galleryPost = null;
		public GalleryPost GalleryPost
		{
			get { return galleryPost; }
			set { galleryPost = value; }
		}

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			AverageRating = FindText( "AverageRating" );
			TotalVotes = FindText( "TotalVotes" );
			RatePost = (GalleryRatePost)FindControl( "RatePost" );
		}

		#endregion

		#region Event Overrides

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			CSContext cSContext = CSContext.Current;
			ApplicationKey = cSContext.ApplicationKey;
			PictureID = cSContext.PostID;
		}
		
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.DataBind();
		}

		public override void DataBind()
		{
			base.DataBind ();
			BindData();
		}

		#endregion

		private void BindData()
		{
			// If we have no settings, return
			if((ApplicationKey == null) || (PictureID == -1))
				return;

			// Check if ratings were enabled
			if(!Galleries.GetGallery(ApplicationKey).EnableRatings)
			{
				this.Visible = false;
				return;
			}

			// Get the picture if we don't have one
			if(galleryPost == null)
				galleryPost = GalleryPosts.GetPicture(PictureID);

			// Setup the Ratings
			if(TotalVotes != null)
				TotalVotes.Text = galleryPost.TotalRatings.ToString();
			
			// Check if the user is allowed to vote/rate
			if(RatePost != null)
			{
				RatePost.IsReadOnly = true;
				RatePost.Post = galleryPost;
			}

			if(AverageRating != null)
			{
				if(galleryPost.TotalRatings > 0)
					AverageRating.Text = ((double)galleryPost.RatingSum / (double)galleryPost.TotalRatings).ToString("0.0#");
				else
					AverageRating.Text = "0";
			}
		}
	}
}
